import timber.controller.App;
import mx.utils.Delegate;

/**
	Page class.
	Acts as a page controller for a section loaded into a movieclip.
	Used most frequently with the PageFactory and App classes to form a simple flash site framework.

	@author Chandler McWilliams
	@version 2005-06-29
*/
dynamic class timber.controller.Page extends MovieClip
{
	
	private var xmlid		= false;
	private var thedata	= false;
	private var initmethod = false;
	private var pageDidRender:Boolean = false;
	
// ===========================================================
// - CONSTRUCTOR
// ===========================================================
	public function Page() {}


// ===========================================================
// - TURN A CLIP INTO THIS OBJECT
// ===========================================================		
	public function init(clip, method) {
		// hold on to xmlid
		var xid = getXMLID();
		
		// do fanciness to make me the class for this clip
		clip.__proto__ = this.__proto__;
		clip.__constructor__ = this.__constructor__;
		this = clip;
		
		// reset xmlid
		setXMLID(xid);
		initmethod = (method)?method:'index';
		
		// hide the clip
		hide();
				
		// load XML for this section if there is any
		if (getXML()) {
			var dao = getDataObject();
			thedata = new dao(getXML(), Delegate.create(this, dataDidLoad));
		} else {
			$render();
		}
	}
	
	public function executeSubSection(method) {
		initmethod = (method)?method:'index';
		show();
		this[initmethod]();
	}

	private function dataDidLoad(ok) {
		if (ok) {
			$render();
		} else {
			// TODO: This should really throw an error.
			App.error('Failed to load xml data from: '+getXML());
		}			
	}
	
	// final method for page class, calls render and initmethod if need be
	private function $render() {
		pageDidRender = true;
		show();
		render();
		if (initmethod) this[initmethod]();
	}
	
	
	// ===========================================================
	// - RENDER
	// ===========================================================
	// to be overridden in subclasses
	private function render() {}
	
	private function index() {}
	
	// ===========================================================
	// - TRANSITION METHODS
	// ===========================================================
	public function requiresOutTransition() { return false; }
	public function beginOutTransition(oldpath, newpath) {};

	public function outTransitionComplete() {
		App.outTransitionComplete();
	}
	
	/**
	Set the xmlid.
	*/
	public function setXMLID(aID) {
		xmlid = aID;
	}

	/**
	 Get the xml ID, used for finding the  XML URL in config.xml
	*/
	public function getXMLID() {
		return xmlid;
	}
	
	/**
	 Get the xml url from config.xml.
	*/
	private function getXML() {
		if (getXMLID() === false) return false;
		return _global[getXMLID().toUpperCase()];
	}

	/**
		Return the data object for this page.
	*/
	private function getDataObject() {
		return timber.data.XMLData;
	}
	
	/**
		Hide and show.
	*/
	function hide() {
		this._visible = false;
	}

	function show() {
		this._visible = true;
	}
	
	/**
		Return boolean if the page has already rendered
	*/
	public function didRender():Boolean {
		return pageDidRender;
	}

}